<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @fliterChang="filterChange" />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sortChange="sortChange" />
        <!-- 列表 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id">
            <GoodsItem :goods="goods" />
          </li>
        </ul>
        <!-- 无限加载组件 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData" />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import SubBread from './components/sub-bread'
import SubFilter from './sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import XtxInfiniteLoading from '@/components/library/xtx-infinite-loading.vue'
import { findSubCategoryGoods } from '@/api/home'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem, XtxInfiniteLoading },
  setup() {
    const route = useRoute()
    const loading = ref(false) // 加载中
    const finished = ref(false) // 是否加载完
    const goodsList = ref([]) //列表数据
    // 请求参数
    let reqParams = {
      page: 1, //页码
      pageSize: 10 //每页大小
    }
    // 获取列表数据
    const getData = () => {
      loading.value = true
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(({ result }) => {
        if (result.items.length) {
          goodsList.value.push(...result.items)
          reqParams.page++
        } else {
          finished.value = true
        }
        loading.value = false
      })
    }
    //更改二级分类时重新加载数据
    watch(
      () => route.params.id,
      (newVal) => {
        // 要判断是否为二级路由和newVal是否存在  从无到有从有到无都会被监听到
        if (newVal && `/category/sub/${newVal}` === route.path) {
          finished.value = false
          goodsList.value = []
          reqParams = {
            page: 1, //页码
            pageSize: 10 //每页大小
          }
        }
      }
    )
    // 更改排序
    const sortChange = (sortParams) => {
      finished.value = false
      goodsList.value = []
      //合并之前的参数 比如 reqParams.categoryId 页码大小等
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      if (goodsList.value.length === 0) {
        // 防止无数据,无限加载组件在可视区不会自动获取数据的问题
        getData()
      }
      console.log(reqParams)
    }

    //更改筛选组件
    const filterChange = (changeFilter) => {
      finished.value = false
      goodsList.value = []
      reqParams = { ...reqParams, ...changeFilter }
      reqParams.page = 1
    }

    return { getData, loading, finished, goodsList, sortChange, filterChange }
  }
}
</script>
<style lang="less" scoped>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
